class = require'lib.middleclass'
Stateful = require'lib.stateful'
loader = require'lib.ATL.trunk.loader'
tmap = require'lib.ATL.trunk.Map'
ttile = require'lib.ATL.trunk.Tile'
require'src.map'
require'src.camera'
require'src.infopunk'
xml = require'lib.xml'

_print = print



function print(...)

	local s = {...}

	for i = 1, #s do
		if s[i] == true then s[i] = "true"
		elseif s[i] == false then s[i] = "false" 
		elseif not s[i] then s[i] = "nil" end

		if type(s[i]) == "userdata" then
			s[i] = "userdata"
		end
		
		_print(os.date("%H:%M:%S")..": "..s[i])
		
		if love.filesystem.exists('log') then
			love.filesystem.append('log', os.date("%H:%M:%S")..": ".. s[i]..'\r\n')
		else
			love.filesystem.write('log', os.date("%H:%M:%S")..": ".. s[i]..'\r\n')
		end
	end
end

function love.load()
	
	loader.path = "assets/maps/"
	infopunk = Infopunk:new()
	
end

function love.update(dt)
	dt = math.min(0.01666667, dt)
	infopunk:update(dt)	
	
end

function love.draw()
	infopunk:draw()
end

function love.keypressed(key,isRepeat)
	infopunk:keypressed(key,isRepeat)
	
end

function love.keyreleased(key)
	
end

function love.mousepressed(x, y, button)
	
end

function love.mousereleased(x, y, button)
	
end

function love.threaderror(thread, errorstr)
	print("Thread Error!\n" .. errorstr)
end

function love.quit()
	
end

function table.val_to_str ( v )
  if "string" == type( v ) then
    v = string.gsub( v, "\n", "\\n" )
    if string.match( string.gsub(v,"[^'\"]",""), '^"+$' ) then
      return "'" .. v .. "'"
    end
    return '"' .. string.gsub(v,'"', '\\"' ) .. '"'
  else
    return "table" == type( v ) and table.tostring( v ) or
      tostring( v )
  end
end

function table.key_to_str ( k )
  if "string" == type( k ) and string.match( k, "^[_%a][_%a%d]*$" ) then
    return k
  else
    return "[" .. table.val_to_str( k ) .. "]"
  end
end

function table.tostring( tbl )
  local result, done = {}, {}
  for k, v in ipairs( tbl ) do
    table.insert( result, table.val_to_str( v ) )
    done[ k ] = true
  end
  for k, v in pairs( tbl ) do
    if not done[ k ] then
      table.insert( result,
        table.key_to_str( k ) .. "=" .. table.val_to_str( v ) )
    end
  end
  return "{" .. table.concat( result, "," ) .. "}"
end
